package com.example.demo.bingfa;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName Recenntlock
 * @Description TODO
 * Author yangqinglin
 * @Date 2021/2/12 17:07
 * @Version
 **/
@Slf4j
public class Recenntlock {
    private  static ReentrantLock lock=new ReentrantLock();


    public static void main(String[] args) throws InterruptedException {

        Thread t1=new Thread(()->{
            try {
                if (!lock.tryLock(2, TimeUnit.SECONDS)){
                    log.debug("获取锁失败！！");
    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("获取锁失败！！");
                return;
            }
            try{

                log.debug("获取锁！！");
            }finally {
                lock.unlock();

            }


        },"t1");
        lock.lock();
        log.debug("获取");
        t1.start();
        Thread.sleep(1000);
        lock.unlock();
        log.debug("释放了锁！");
    }
}
